const dayjs = require('dayjs');

const { findUserById } = require('../controller/user');
const { getTaskById } = require('../controller/task');
const reminder = require('../lib/reminder');

module.exports = async function taskRemind(ctx) {
  const userId = ctx.session.userInfo.id;
  const taskId = ctx.state.taskId;
  const { email } = await findUserById(userId);
  const { content, remindTime } = await getTaskById(taskId);

  if (remindTime) {
    if (dayjs().isBefore(dayjs(remindTime))) {
      return await reminder.addTaskReminder(userId, taskId, email, content, remindTime);
    }
  } else {
    return await reminder.removeTaskReminder(userId, taskId);
  }
};
